package com.example.controller;

import cn.hutool.json.JSONUtil;
import com.example.common.HttpStatusEnum;
import com.example.entity.ResponseData;
import com.example.pojo.Essay;
import com.example.service.EssayService;
import com.example.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文章信息表(Essay)表控制层
 *
 * @email: pengyujun53@163.com
 * @author: peng_YuJun
 * @date: 2024/01/17
 * @time: 23:13
 */
@RestController
@RequestMapping("essay")
@Api(tags = "Essay信息请求类")
public class EssayController {
    /**
     * 服务对象 
     */
    @Resource
    private EssayService essayService;

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param essay 筛选条件
     * @return 查询结果
     */
    @ApiOperation("分页查询")
    @GetMapping("getPage")
    public ResponseData getPage(Integer currentPage, Integer pageSize, Essay essay) {
        if (currentPage == null || pageSize == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return essayService.getByPage(currentPage, pageSize, essay);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("根据ID查询信息")
    @GetMapping("findById")
    public ResponseData findById(Long id) {
        if (id == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(),HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return essayService.findById(id);
    }

    /**
     * 新增数据
     *
     * @param  essay 实体
     * @return 新增结果
     */
    @ApiOperation("增加")
    @PostMapping("add")
    public ResponseData add(MultipartFile file, Essay essay) {
        if(essay == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return essayService.add(file,essay);
    }

    /**
     * 更新数据
     *
     * @param essay 实体
     * @return 更新结果
     */
    @ApiOperation("更新")
    @PostMapping("update")
    public ResponseData update(MultipartFile file, Essay essay) {
        if(essay == null || essay.getId() == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return essayService.update(file, essay);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 
     */
    @ApiOperation("删除")
    @PostMapping("delete")
    public ResponseData delete(Long id) {
        if (id == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return essayService.delete(id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation("批量删除")
    @PostMapping("deleteBatch")
    public ResponseData deleteBatch(String ids){
        List<Long> idList = JSONUtil.toList(ids, Long.class);
        if (idList == null || idList.size()<=0){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return essayService.deleteBatch(idList);
    }

    /**
     * 上传文章图片
     * @param file
     * @return
     */
    @ApiOperation("上传文章图片")
    @PostMapping("uploadEssayPic")
    public ResponseData uploadEssayPic(MultipartFile file) {
        if (!FileUtil.isMultipartFile(file)){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return essayService.uploadEssayPic(file);
    }

    /**
     * 获取全部正式发布的文章
     * @return
     */
    @ApiOperation("获取全部正式发布的文章")
    @GetMapping("getAllValidEssay")
    public ResponseData getAllValidEssay() {
        return essayService.getAllValidEssay();
    }

    /**
     * 统计疾病数
     * @return
     */
    @ApiOperation("统计文章数")
    @GetMapping("countAll")
    public ResponseData countAll(){
        return essayService.countAll();
    }
}

